﻿
/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.Collections;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using PowerShell.Host.PsTFS.CdmLet.PsObject;
using System.IO;

namespace PowerShell.Host.PsTFS.CdmLet
{
    /// <summary>
    /// Ajout d'une branche
    /// </summary>
    [Cmdlet(VerbsCommon.Add, "Branch")]
    public class Add_Branch : Cmdlet
    {
        public Add_Branch()
        {
            if (TFSProvider.s_TFSDriveInfo != null)
            {
                this.TFS = TFSProvider.s_TFSDriveInfo.TFS;
                this.ProjectName = TFSProvider.s_TFSDriveInfo.ProjectName;
            }
        }
        protected override void ProcessRecord()
        {
            if (TFS == null)
            {
                WriteError(new ErrorRecord(new ArgumentException("No server TFS!!!"), "PsTFS", ErrorCategory.ObjectNotFound, TFS));
                return;
            }
            if (ProjectName == null)
            {
                WriteError(new ErrorRecord(new ArgumentException("No server ProjectName!!!"), "PsTFS", ErrorCategory.ObjectNotFound, TFS));
                return;
            }

            int changesetNumber = 0;

            try
            {
                changesetNumber = PsMethode.AddBranche(this.Description,this.Source,this.Target,this.ProjectName,this.TFS);
            }
            catch (Exception ex)
            {
                ErrorRecord err = new ErrorRecord(ex, "<ID>", ErrorCategory.WriteError, null);
                WriteError(err);
                PsLog.AddLog(ex.StackTrace);
            }

            WriteObject(changesetNumber);

        }

        

        protected override void EndProcessing()
        {
            PsLog.AddLog(null, "Add-Branch is end");
            base.EndProcessing();
        }

        /// <summary>
        /// Object contenant Team Foundation Server 2008
        /// </summary>
        [Parameter(Position = 0)]
        public TeamFoundationServer TFS { get; set; }

        /// <summary>
        /// Nom du projet
        /// </summary>
        [Parameter(Position = 1)]
        public string ProjectName { get; set; }

        /// <summary>
        /// Source depuis laquelle effectuer la branche
        /// </summary>
        [Parameter(Position = 2, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string Source { get; set; }

        /// <summary>
        /// Destination de la branche
        /// </summary>
        [Parameter(Position = 3, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string Target { get; set; }

        /// <summary>
        /// Description pour la creation de la branche
        /// </summary>
        [Parameter(Position = 4, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string Description { get; set; }


        
    }
}


